from pyspark.sql import SparkSession
import os
import pyspark.sql.functions as F

from cn.itcast.tag.base.BaseModel import BaseModel
from cn.itcast.tag.bean.ESMeta import ruleToESMeta

"""
-------------------------------------------------
   Description :	TODO：
   SourceFile  :	GenderModel1
   Author      :	itcast team
-------------------------------------------------
"""

# 0.设置系统环境变量
os.environ['JAVA_HOME'] = '/export/server/jdk1.8.0_241/'
os.environ['SPARK_HOME'] = '/export/server/spark'
os.environ['PYSPARK_PYTHON'] = '/root/anaconda3/envs/pyspark_env/bin/python3'
os.environ['PYSPARK_DRIVER_PYTHON'] = '/root/anaconda3/envs/pyspark_env/bin/python3'

#todo 性别
class GenderModel1(BaseModel):
    def compute(self, es_df, five_df):
        """
        标签计算逻辑实现方法，将业务数据与五级标签规则进行关联，生成用户性别标签

        Args:
            es_df: 从ES读取的业务数据，包含用户基本信息，如id和gender
            five_df: 从MySQL读取的五级标签规则数据，包含id和rule字段

        Returns:
            DataFrame: 包含userId和tagsId的标签计算结果
        """
        # 打印es_df的schema和示例数据，用于调试和查看数据结构
        es_df.printSchema()
        es_df.show()

        # 打印five_df的schema和示例数据，用于调试和查看五级标签规则结构
        five_df.printSchema()
        five_df.show()

        # 将业务数据es_df与五级标签规则five_df进行左连接：
        # 连接条件为：用户的性别(gender)匹配五级标签规则(rule)
        # 选择输出字段：
        #   - id字段别名为userId，表示用户ID
        #   - rule对应的id字段别名为tagsId，表示匹配到的性别标签ID
        new_df = es_df.join(other=five_df,
                            on=es_df['gender'] == five_df['rule'],
                            how='left').select(es_df['id'].alias("userId"), five_df['id'].alias("tagsId"))

        # 返回计算结果new_df
        return new_df


if __name__ == '__main__':
    genderModel = GenderModel1(4)
    genderModel.execute()
